using System;
using System.Windows.Forms;
using Microsoft.Practices.CompositeUI.SmartParts;
using Microsoft.Practices.ObjectBuilder;
using Jbe.TestSuite.Infrastructure.Interface;
using Jbe.TestSuite.TestDevice.Manager.Constants;
using Jbe.TestSuite.TestDevice.Manager.Interface.Services;
using Jbe.TestSuite.TestDevice.Manager.Mapper;
using System.Collections.Generic;
using Microsoft.Practices.CompositeUI.Commands;
using SCSFContrib.CompositeUI.WinForms.UIElements;
using SCSFContrib.CompositeUI.WinForms;

namespace Jbe.TestSuite.TestDevice.Manager.Views
{
    [SmartPart]
    public partial class TestDevicesView : UserControl, ITestDevicesView
    {
        [CreateNew]
        public TestDevicesViewPresenter Presenter
        {
            set
            {
                presenter = value;
                presenter.View = this;

                presenter.WorkItem.UIExtensionSites.RegisterSite(UIExtensionSiteNames.NewDevice, new ToolStripDropDownItemUIAdapter(newDeviceToolStripDropDownButton));

                CommandHelper.AddInvoker(presenter.WorkItem, CommandNames.ShowDevice, openToolStripButton, openToolStripMenuItem);
                CommandHelper.AddInvoker(presenter.WorkItem, CommandNames.CloseDevice, removeToolStripButton, removeToolStripMenuItem);
                UpdateUI();
            }
        }

        public TestDevicesView()
        {
            InitializeComponent();
        }

        public void UpdateControlViews(IList<IControlView> controlViews) 
        {
            listView.BeginUpdate();
            listView.Items.Clear();
            listView.Groups.Clear();
            foreach (IControlView controlView in controlViews) 
            {
                ListViewItem item = ControlViewMapper.ToListViewItem(controlView);

                if (item.Group != null)
                {
                    ListViewGroup existingGroup = null;
                    foreach (ListViewGroup group in listView.Groups)
                    {
                        if (group.Header == item.Group.Header) { existingGroup = group; }
                    }

                    if (existingGroup != null)
                    {
                        item.Group = existingGroup;
                    }
                    else
                    {
                        listView.Groups.Add(item.Group);
                    }
                }
                listView.Items.Add(item);
            }
            listView.EndUpdate();

            ListViewSelectedIndexChanged();
        }

        protected override void OnLoad(EventArgs e)
        {
            presenter.OnViewReady();
            base.OnLoad(e);
        }

        private void ListViewSelectedIndexChanged(object sender, EventArgs e)
        {
            ListViewSelectedIndexChanged();
        }

        private void ListViewSelectedIndexChanged()
        {
            UpdateUI();

            IList<IControlView> controlViews = new List<IControlView>(listView.SelectedItems.Count);
            foreach (ListViewItem item in listView.SelectedItems)
            {
                controlViews.Add(ControlViewMapper.FromListViewItem(item));
            }
            presenter.UpdateSelectedControlViews(controlViews);
        }

        private void ListViewDoubleClick(object sender, EventArgs e)
        {
            presenter.WorkItem.Commands[CommandNames.ShowDevice].Execute();
        }

        private void UpdateUI()
        {
            bool enabled = (listView.SelectedItems.Count > 0);
            CommandHelper.SetStatus(presenter.WorkItem, CommandNames.ShowDevice, enabled);
            CommandHelper.SetStatus(presenter.WorkItem, CommandNames.CloseDevice, enabled);
        }
    }
}

